package com.dingup.api.Util;



import com.suyeer.cache.RedisUtil;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D.Double;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * Created by wancheng on 2018/8/24.
 */
public class GCheckCodeUtil {
    private static String CHECK_CODE_SESSION_NAME = "RAND_CHECK_CODE_";
    private static Integer IMG_WIDTH = Integer.valueOf(80);
    private static Integer IMG_HEIGHT = Integer.valueOf(25);
    private static Integer TRANSLATION_TX = Integer.valueOf(-3);
    private static Integer TRANSLATION_TY = Integer.valueOf(5);
    private static float SCALE_SIZE_MIN = 0.8F;
    private static float SCALE_SIZE_MAX = 1.0F;

    public GCheckCodeUtil() {
    }

    public static void setImageParameters(Integer width, Integer height, Integer translationTx, Integer translationTy, float scaleSizeMin, float scaleSizeMax) {
        IMG_WIDTH = width;
        IMG_HEIGHT = height;
        TRANSLATION_TX = translationTx;
        TRANSLATION_TY = translationTy;
        SCALE_SIZE_MIN = scaleSizeMin;
        SCALE_SIZE_MAX = scaleSizeMax;
    }

    public static void generateRandomCode(HttpServletRequest request, HttpServletResponse response,String account) throws ServletException, IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "No-cache");
        response.setDateHeader("Expires", 0L);
        response.setContentType("image/jpeg");
        BufferedImage image = new BufferedImage(IMG_WIDTH.intValue(), IMG_HEIGHT.intValue(), 1);
        Graphics g = image.getGraphics();
        Graphics2D g2d = (Graphics2D)g;
        Random random = new Random();
        Font mfont = new Font("楷体", 1, 22);
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, IMG_WIDTH.intValue(), IMG_HEIGHT.intValue());
        g.setFont(mfont);
        g.setColor(getRandColor(180, 200));

        int itmp;
        int session;
        for(int sRand = 0; sRand < 100; ++sRand) {
            int ctmp = random.nextInt(IMG_WIDTH.intValue() - 1);
            itmp = random.nextInt(IMG_HEIGHT.intValue() - 1);
            session = random.nextInt(6) + 1;
            int color = random.nextInt(12) + 1;
            BasicStroke g2d_word = new BasicStroke(2.0F, 0, 2);
            Double trans = new Double((double)ctmp, (double)itmp, (double)(ctmp + session), (double)(itmp + color));
            g2d.setStroke(g2d_word);
            g2d.draw(trans);
        }

        String var15 = "";
        String var16 = "";
        boolean var17 = false;

        for(session = 0; session < 4; ++session) {
            switch(random.nextInt(3)) {
                case 1:
                    itmp = random.nextInt(26) + 65;
                    var16 = String.valueOf((char)itmp);
                    break;
                case 2:
                default:
                    itmp = random.nextInt(10) + 48;
                    var16 = String.valueOf((char)itmp);
            }

            var15 = var15 + var16;
            Color var19 = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), random.nextInt(110));
            g.setColor(var19);
            Graphics2D var20 = (Graphics2D)g;
            AffineTransform var21 = new AffineTransform();
            var21.setToTranslation((double)TRANSLATION_TX.intValue(), (double)TRANSLATION_TY.intValue());
            float scaleSize = random.nextFloat() + SCALE_SIZE_MIN;
            if(scaleSize > SCALE_SIZE_MAX) {
                scaleSize = SCALE_SIZE_MAX;
            }

            var21.scale((double)scaleSize, (double)scaleSize);
            var20.setTransform(var21);
            g.drawString(var16, 16 * session + 28, 14);
        }
        ImageVerifyCodeUtil.getInstance().putCode(account, var15);
        g.dispose();
        ImageIO.write(image, "JPEG", response.getOutputStream());
    }

    public static Boolean checkCode(HttpServletRequest request, String code) {
        return checkCode(request, code, Boolean.valueOf(false));
    }

    public static Boolean checkCode(HttpServletRequest request, String code, Boolean ifDelCode) {
        HttpSession session = request.getSession(true);
        if(session.getAttribute(CHECK_CODE_SESSION_NAME) == null) {
            return Boolean.valueOf(false);
        } else {
            String randomCode = session.getAttribute(CHECK_CODE_SESSION_NAME).toString();
            if(ifDelCode.booleanValue()) {
                session.removeAttribute(CHECK_CODE_SESSION_NAME);
            }

            return Boolean.valueOf(code.equalsIgnoreCase(randomCode));
        }
    }

    private static Color getRandColor(int s, int e) {
        Random random = new Random();
        short s1;
        s = s > 255?(s1 = 255):s;
        short e1;
        e = e > 255?(e1 = 255):e;
        int r = s + random.nextInt(e - s);
        int g = s + random.nextInt(e - s);
        int b = s + random.nextInt(e - s);
        return new Color(r, g, b);
    }

    private static String getHanZi() {
        String ret = "";
        String[] rBase = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

        try {
            Random random = new Random();
            int r1 = random.nextInt(3) + 11;
            String str_r1 = rBase[r1];
            int r2;
            if(r1 == 13) {
                r2 = random.nextInt(7);
            } else {
                r2 = random.nextInt(16);
            }

            String str_r2 = rBase[r2];
            int r3 = random.nextInt(6) + 10;
            String str_r3 = rBase[r3];
            int r4;
            if(r3 == 10) {
                r4 = random.nextInt(15) + 1;
            } else if(r3 == 15) {
                r4 = random.nextInt(15);
            } else {
                r4 = random.nextInt(16);
            }

            String str_r4 = rBase[r4];
            byte[] bytes = new byte[2];
            String str_12 = str_r1 + str_r2;
            int tempLow = Integer.parseInt(str_12, 16);
            bytes[0] = (byte)tempLow;
            String str_34 = str_r3 + str_r4;
            int tempHigh = Integer.parseInt(str_34, 16);
            bytes[1] = (byte)tempHigh;
            return new String(bytes, "GBK");
        } catch (Exception var16) {
            return ret;
        }
    }
}

